package com.webchat.client.mall.controller;


import com.webchat.client.mall.service.MallProductOrderService;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidateLogin;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.mall.CreateProductPaymentOrderReqVO;
import com.webchat.domain.vo.request.mall.PreCreateProductOrderReqVO;
import com.webchat.domain.vo.response.mall.MallUserProductOrderResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mall-service/product/order")
public class MallProductOrderController {


    @Autowired
    private MallProductOrderService mallProductOrderService;

    /**
     * ”立即购买“ ==> 预创建订单（包含库存预扣减）
     *
     * @param preCreateProductOrderReq
     * @return
     */
    @ValidateLogin
    @PostMapping("/preCreate")
    public APIResponseBean<String> preCreateOrder(@RequestBody PreCreateProductOrderReqVO preCreateProductOrderReq) {

        // TODO 参与校验

        String loginUserId = SessionHelper.getCurrentUserId();
        preCreateProductOrderReq.setUserId(loginUserId);
        String orderNo = mallProductOrderService.preCreate(preCreateProductOrderReq);
        return APIResponseBeanUtil.success(orderNo);
    }

    /**
     * 获取用户订单详情
     *
     * @param orderNo
     * @return
     */
    @ValidateLogin
    @GetMapping("/userOrder/{orderNo}")
    public APIResponseBean<MallUserProductOrderResponseVO> userOrder(@PathVariable String orderNo) {
        String userId = SessionHelper.getCurrentUserId();
        MallUserProductOrderResponseVO userOrder = mallProductOrderService.getUserOrder(userId, orderNo);
        return APIResponseBeanUtil.success(userOrder);
    }

    /**
     * 创建商品订单对应的支付订单
     *
     * @param createProductPaymentOrderReqVO
     * @return
     */
    @ValidateLogin
    @PostMapping("/payment/createOrder")
    public APIResponseBean<String> createPaymentOrder(@RequestBody CreateProductPaymentOrderReqVO createProductPaymentOrderReqVO) {

        // TODO 参数校验
        String userId = SessionHelper.getCurrentUserId();
        createProductPaymentOrderReqVO.setUserId(userId);
        String paymentOrderId = mallProductOrderService.createPaymentOrder(createProductPaymentOrderReqVO);
        return APIResponseBeanUtil.success(paymentOrderId);
    }


}
